package com.atguigu.usercenterservice.service.impl;

import com.atguigu.commonutils.utils.JwtUtils;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.usercenterservice.entity.UcenterMember;
import com.atguigu.usercenterservice.entity.Vo.LoginVo;
import com.atguigu.usercenterservice.entity.Vo.RegisterVo;
import com.atguigu.usercenterservice.mapper.UcenterMemberMapper;
import com.atguigu.usercenterservice.service.UcenterMemberService;
import com.atguigu.usercenterservice.utils.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author kaixuan
 * @since 2020-06-10
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 用户登陆
     * @param loginVo
     * @return
     */
    @Override
    public String login(LoginVo loginVo) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"手机号或密码为空");
        }
        QueryWrapper<UcenterMember> query = new QueryWrapper<UcenterMember>();
        query.eq("mobile",mobile);
        UcenterMember user = baseMapper.selectOne(query);
        if (null == user){
            throw new GuliException(20001,"用户不存在");
        }
        if (user.getIsDisabled()){
            throw new GuliException(20001,"用户已被禁用");
        }
        //判断密码
        if (!MD5.encrypt(password).equals(user.getPassword())){
            throw new GuliException(20001,"密码不正确，登陆失败");
        }
        String token = JwtUtils.getJwtToken(user.getId(),user.getNickname());
        return token;
    }

    /**
     * 用户注册
     * @param registerVo
     * @return
     */
    @Override
    @Transactional
    public void register(RegisterVo registerVo) {
        String mobile = registerVo.getMobile();
        String code = registerVo.getCode();
        String password = registerVo.getPassword();
        String nickName = registerVo.getNickname();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(code) || StringUtils.isEmpty(password) || StringUtils.isEmpty(nickName)){
            throw new GuliException(20001,"请补全信息");
        }
        //校验校验验证码
        //从redis获取发送的验证码
        String mobleCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(mobleCode)){
            throw new GuliException(20001,"验证码错误");
        }
        // 检查数据库手机号是否重复
        QueryWrapper<UcenterMember> query = new QueryWrapper<UcenterMember>();
        query.eq("mobile",mobile);
        UcenterMember user = baseMapper.selectOne(query);
        if (null != user){
            throw new GuliException(20001,"此手机号已注册");
        }
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickName);
        member.setPassword(MD5.encrypt(password));
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");//默认头像
        member.setIsDisabled(false);
        baseMapper.insert(member);
    }

    /**
     * 根据日期查询注册人数
     * @param day
     * @return
     */
    @Override
    public Integer selectRegisterCount(String day) {
        return baseMapper.selectRegisterCount(day);
    }


}
